0

Can someone tell me how I can go about converting this curl code listed below to $http.post() for angular js? Any help would be much appreciated. Thanks!

curl https://example.zendesk.com/api/v2/organizations.json \
 -H "Content-Type: application/json" \
 -d '{"organization": {"name": "My Organization"}}' \
 -v -u {email_address}/token:{api_token}

So far I have the following angularjs code set up:

                     $http({
                            method : "POST",
                            url : url,
                            data : angular.toJson($scope.form),
                            headers : {
                                'Content-Type' : 'application/json'
                            }
                        }).then(Success,Failure);

I just have no idea how to setup the email address and token with the call.

4
  • What you have tried so far in the attempt to convert towards $http.post() ? Commented Oct 27, 2016 at 19:14
  • I have updated my question with what I have tried so far. Commented Oct 27, 2016 at 19:20
  • You can't and you shouldn't. You really don't want to expose your api token/key. You should create a back-end service that will forward that information. Commented Oct 27, 2016 at 19:26
  • I have a backend service set up to call the api token when needed so thats not gonna be much of a problem. What I need is to just convert this curl for now... Commented Oct 27, 2016 at 19:28

1 Answer 1

2

I think the best advice I could give you is to refrain from making CORS requests on the front-end. You want to have separation of concerns and it would be better for your back-end to call the api token and then parse that data into JSON. In the end, you have a back-end endpoint/route in which the front-end can send a request for data.

In your back-end, the route you have set up will look something like this:

router.post('/api/data', (req, res) => {
    request('https://example.zendesk.com/api/v2/organizations.json', {
        headers: { ... }, token: { ... }
    }).then((result) { res.json(result) };
});

In your Angular factory/service, you'll be making a HTTP request like so:

$http.post('localhost/api/data')
    .then((response) => { response = response.data })
    .catch((error) => { if (err) { console.log(err) })

I hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.