Archived Forum Post

Index of archived forum posts

Question:

Sending json struct with http request

Sep 02 '13 at 10:25

I am currently sending json data successfully to a 3rd party API: http://sendgrid.com/docs/API_Reference/Web_API/mail.html using req.AddParam like this:

  req.AddParam("to","xxx")
  req.AddParam("subject","xxx")
  req.AddParam("from","xxx")

I am trying to use a different service that uses a struct for the parameters: https://mandrillapp.com/api/docs/messages.JSON.html#method=send

I am having trouble figuring out how to make this work with req.AddParam. Should it work like this?

req.AddParam("message", "{"html": "

Example HTML content

","text": "Example text content","subject": "example subject"}")


Answer

When using AddParam, you're not actually sending a JSON request. You must be sending either an application/x-www-form-urlencoded or a multipart/form-data request. It must be that for "sendgrid.com", it accepts either JSON HTTP requests in addition to the other standard ways of providing name/value params.

For mandrillapp.com, I suspect it's only accepting JSON requests -- where the Content-Type is either application/jsonrequest or application/json, and the body of the HTTP request is the JSON struct itself. You would achieve this by calling either PostJson or PostJson2.