Question

I am using TRAK api.

It says we need to send a http request with credentials in as body. He told me a way to do is to go to Hurl Select post, add address as https://trak.addy.co/api/v1/auth/keys And then click Add Body in Parameters. And use following as body parameter.

{
   "email":"m@addy.co",
   "password":"foo*bar!",
   "name":"addy-dev"
}

This works in HURL and returns me the api key.

But when I am sending it via php it is not working.

$r = new HttpRequest('https://trak.addy.co/api/v1/auth/keys', HttpRequest::METH_POST);
$r->addPostFields(array('email' => 'm@addy.co', 'pass' => 'foo*bar!', 'name' => 'addy-dev'));

try {
    echo $r->send()->getBody();
} catch (HttpException $ex) {
    echo $ex;
}

I guess this is because it is sending the parameters instead of body, Correct?

How can I send the body in http request?

If anyone know how to do it via jQuery then it will be helpful.

Was it helpful?

Solution

It looks like you need to add your paramters to the body of your request using.

$r->setBody('[{ "email":"m@addy.co", "password":"foo*bar!", "name":"addy-dev" }]');

As long the other side receiving will process the parameters sent in the body, this should work, otherwise it wont.

You may also want to send your request as JSON by adding the following line(Not sure if requirement).

$r->addHeaders(array('Content-Type' => 'application/json'));

EDIT: fixed syntax error of addHeaders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top