How to check if request type is JSON in a Sinatra app? - sinatra

I need to parse a request body as JSON, but how do I check if the request is a JSON request?

Have you tried request.content_type?
But the response could be a plain text and still be a valid JSON, so I think it would be valid to get the contents and try to parse it.

Related

Postman API Invalid Content-Type Header

I'm trying to test and API call for the Smartsheet API in Postman, but I keep receiving the same error. Even though I am defining the correct Content-Type as per the API documentation, the response I get is the error 1124, which is an invalid content-type header. I haven't been able to figure out exactly what is causing the issue. I have tried typing the header in the address and in the header tab in Postman, but neither option has given me the result I wanted.
Postman Screenshot
If I run the same request in Postman as your screenshot shows (except using my sheet ID), with the same headers that you're using, my request succeeds. i.e., if you're sending the Content-Type header with value application/json for this request, you should not be receiving the error message that you're reporting (1124 - invalid content-type header).
You would, however, receive that error message if you were either not including the Content-Type header at all OR if you were specifying an invalid value for the Content-Type header. A suggestion for troubleshooting: use Fiddler (or a similar tool) to examine the request that's being sent over the wire when you execute this request in Postman -- does it include the Content-Type header, and if so, what's the value of that header?
UPDATE:
Thanks for adding a screenshot of the body you're setting in Postman -- I believe that's the source of your issue. i.e., your Content-Type header says that the request body is in JSON format, but you're actually not sending JSON. To fix this: instead of specifying key/value pairs in Postman, select the raw radio button and specify the body in JSON format. Here's a screenshot of what that looks like:

How to Print the DataHandler Content for SOAP Request

I have the object of "javax.activation.DataHandler.DataHandler"
and i saw that it has been added to SOAP request using the below command
org.apache.axiom.util.stax.XMLStreamWriterUtils.writeDataHandler()
I want to get the content from DataHandler, the content which is actually will be the part of the SOAP request, as i know it will be in the form of string.
thanks in advance!

How to remove HTML content from REST API response?

I have started to work with REST APIs - specifically JIRA REST APIs.
I'm using the API to get response to a JIRA query but the JSON response I am getting contains lot of HTML chunk inside it.
It is not clean enough as showcased here
How can I use it in such a way to get a proper JSON response?
I think, JIRA response in JSON format only if request is successful, if request has any error than it comes in HTML response. actually that html belongs to the JIRA's error page. So correct your request, you will not get any response in HTML fomat. :)

How can I send a POST request with JSON Data in ASIHttpRequest?

I use the ASIHttpRequest in my ios project, and my workmate use the Tastypie as the server, he told me I must post a String with JSON format as Http body(use application/json), but I can't find this Content-Type besides "application/x-www-form-urlencoded" and "multipart/form-data".
then How can I send a POST request with JSON format in ASIHttpRequest?

Json Parsing Problem with Dotnet webserivces

I am posting some values to dotnet webservice.For this i am using AsiFormDataRequest.Values are posted suceesfully but coming response is in xml format. so iam not able to get the success or failure message in json format.
What i want to do for getting json response after posting to server.
Thanking in advance.
You need to configure the server to respond with JSON instead of XML. There is nothing you can do about this from within your app. If you don't have control over the server then you need to forget JSON and parse the XML instead.