RejectedSoftware Forums

Sign up

POSTing data to REST interface

Hi,

I have a generated rest interface and some functions should accept POST data.

This is a part of my interface:

Json getJson(HTTPServerRequest req, HTTPServerResponse res)
{
  return req.json;
} 

interface IRESTApi
{
  @method(HTTPMethod.POST)
  @path("/rest/job/:id/configure/")
  @before!getJson("data")
  @property Json configureJob(string _id, Json data);
}

According to documentation, HTTPServerRequest.json should contain the parsed Json of a JSON request. How does such a JSON request looks like? I tried to send it in many possible forms but the json is always undefined.

I've tried variations of this:

$.ajax({
      type: "POST",
      url: addr,
      data: JSON.stringify({"hello":"world"}),
      dataType: "json"
  })

and this

$.ajax({
      type: "POST",
      url: addr,
      data: {"hello":"world"},
      dataType: "json"
  })

but to no awail.

Any help is appreciated.

Thanks,
Drasha

Re: POSTing data to REST interface

On Sun, 18 Jan 2015 22:03:32 GMT, Drasha wrote:

Hi,

I have a generated rest interface and some functions should accept POST data.

This is a part of my interface:

Json getJson(HTTPServerRequest req, HTTPServerResponse res)
{
  return req.json;
} 

interface IRESTApi
{
  @method(HTTPMethod.POST)
  @path("/rest/job/:id/configure/")
  @before!getJson("data")
  @property Json configureJob(string _id, Json data);
}

According to documentation, HTTPServerRequest.json should contain the parsed Json of a JSON request. How does such a JSON request looks like? I tried to send it in many possible forms but the json is always undefined.

I've tried variations of this:

$.ajax({
      type: "POST",
      url: addr,
      data: JSON.stringify({"hello":"world"}),
      dataType: "json"
  })

and this

$.ajax({
      type: "POST",
      url: addr,
      data: {"hello":"world"},
      dataType: "json"
  })

but to no awail.

Any help is appreciated.

Thanks,
Drasha

It could help to have the request string Vibe.d receive. I think you can get it by setting the loglevel (http://vibed.org/api/vibe.core.log/setLogLevel). If not, you could just fire something like Wireshark.

Beside that, did you change http://vibed.org/api/vibe.http.server/HTTPServerOption ? By default it should just work.

Re: POSTing data to REST interface

On Mon, 19 Jan 2015 06:10:42 GMT, Mathias LANG wrote:

It could help to have the request string Vibe.d receive. I think you can get it by setting the loglevel (http://vibed.org/api/vibe.core.log/setLogLevel). If not, you could just fire something like Wireshark.

Beside that, did you change http://vibed.org/api/vibe.http.server/HTTPServerOption ? By default it should just work.

Even on trace, it does not show body of the request. Nevertheless, here's the trace output:

Date: Mon, 19 Jan 2015 07:49:47 GMT
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/
35.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: cs,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://127.0.0.1:8080/
Content-Length: 11
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
--------------------
Got request header.
evbuffer_read 11 bytes (fd 248)
 .. got 11 bytes
read data
persist: true
handle request (body 0)
route match: /rest/job/a03e9532-d4db-4afc-9507-6f8c6863fa0f/configure/ -> POST /
rest/job/:id/configure/ ["a03e9532-d4db-4afc-9507-6f8c6863fa0f"]
param _id a03e9532-d4db-4afc-9507-6f8c6863fa0f

and using Tamper Data, I can see that there is a post parameter name "hello" with post parameter value "world".

I have not touched HTTPServerOption, but when you mentioned it, I tried setting parseJsonBody, but that did not change anything.

auto settings = new HTTPServerSettings();
settings.options |= HTTPServerOption.parseJsonBody;

Re: POSTing data to REST interface

Ok, got it now...

The ajax request should have a type "application/json" and not the default jQuery one "application/x-www-form-urlencoded" and the data must be passed as a string, i.e.

$.ajax({
  type: "POST",
  url: addr,
  data: JSON.stringify({"hello":{"cruel":"world"}}),
  contentType: "application/json"
})