How to format the spring rest data json output - json

I'm using Spring Data Rest for reading the records from amazon dynamodb. The service works fine except for output format.
The output I see from the service is -
---> HTTP GET
---> END HTTP (no body)
<--- HTTP 200 (1082ms)
: HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/hal+json
X-Application-Context: application
Server: Jetty(8.1.14.v20131031)
{"_links":{"search":{"href":"something"}},"_embedded":{"userInfoes":[{"id":"bde0ef42-30c2-4ccd-8b0f-44d718827f69","username":"dsds","password":"1234","highestscore":99,"_links":{"self":{"href":"something"}}}]}}
<--- END HTTP (291-byte body)
But I need it in the below format[where no extra "_embedded" and "_links" tags are present] to retrieve the values easily. I'm using retrofit in the service layer. Please help!
[{"id":"bde0ef42-30c2-4ccd-8b0f-44d718827f69","username":"dsds","password":"1234","highestscore":99,}]

Related

JSON in HTTP request to API using JMeter

I am trying to test a web API using JMeter. The web server interacts between JMeter and a database. The server is supposed to take parameters from the JSON in HTTP POST requests and parse them into queries.
The code of the web server in the relevent controller looks like this:
[HttpPost]
[Route("my_awesome_route_path")]
public async Task<List<QueryResult>> invoice([FromBody]InvoiceInfo invoiceInfo)
{
query = string.Format("INSERT INTO my_awesome_table (`my`,`awesome`,`columns`) " +
"VALUES ({0},{1},{2});", invoiceinfo.awesomeparam1, ... ,invoiceinfo.awesomeparamn);
noRowsInserted = await db.Database.ExecuteSqlCommandAsync(query);
And In my JMeter HTTP request form I entered the correct path for the above controller function. I left the parameters and files upload tabs empty while filling the body data tab with the following:
${__FileToString(/my/awesome/file/path${__eval({awesome_json_file_name_with_no_dottxt_in_end_altough_it_has_it})}.txt,,)}
Running the server and with no body data does reach the function to a breakpoint I set. So I know the route path is fine. Adding the above code to the body data causes a 415 error - Unsupported Media Type. Here is the httpsample provided by an output file by JMeter:
<httpSample t="8023" it="0" lt="8023" ct="2" ts="1544434665189" s="false" lb="HTTP Request" rc="415" rm="Unsupported Media Type" tn="Thread Group 1-3" dt="text" by="382" sby="281" ng="1" na="1">
<responseHeader class="java.lang.String">HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: todays_awesome_date
Content-Length: 103
</responseHeader>
<requestHeader class="java.lang.String">Connection: keep-alive
Content-Length: 37
Content-Type: multipart/form-data; boundary=jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH
Host: awesome_ip_add
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_191)
</requestHeader>
<responseData class="java.lang.String">{"Message":"The request entity's media type 'multipart/form-data' is not supported for this resource.'}</responseData>
<cookies class="java.lang.String"></cookies>
<method class="java.lang.String">POST</method>
<queryString class="java.lang.String">--jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH--
</queryString>
<java.net.URL>awesome_route_path</java.net.URL>
</httpSample>
The issue is also not with the sql syntax. It run fine from another function in the controller when parameters were given through the URL.
I also tried filling the files upload with the path and a paramter name and then in parameters to add the file and It always gives out the same error code. I just want the JSON parameters to fill the function's argument so it can be used as shown in code. What is it I'm missing? Thanks.
Add HTTP Header Manager as a child of the HTTP Request sampler
Configure it to send Content-Type header with the value of application/json
Make sure you have Use multipart/form-data for POST NOT CHECKED in the HTTP Request sampler
More information: REST API Testing - How to Do it Right

How to change the Content-Type response in the REST DataSnap

I have a project made from WebBroker REST, running as a service, I need to change theCotent-Type response to application/json and by default WebBroker REST brings me as a result the
Content-Type 'text/html; charset=ISO-8859-1'
I change the response by accessing the method
GetInvocationMetadata(True).ResponseContentType = 'application/json'
of the Data.DBXPlatform class, but it still does not resolve it it adds anotherContent-Type just below the current one.
This only happens only in WebBroker REST, if I create a project by DataSnap Server I usually get it. But I need WebBroker REST to access the information the client sends me.
Example of how to make the problem occur.
Create a Project from the DataSnap REST Application
In the ServerMethods class, do the same model.
uses System.StrUtils, Data.DBXPlatform;
function TServerMethods1.EchoString(Value: string): string;
begin
Result := Value;
GetInvocationMetadata.ResponseContentType := 'application/json';
end;
Example response Header.
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-Length: 25
Date: Tue, 10 Sep 2013 16:41:37 GMT
Pragma: dssession=542354.126073.592372,dssessionexpires=1200000
Content-Type: application/json
In New DataSnap WebBroker Application Wizard you have the option to create a separate Server Module. In the generated unit WebModuleUnit1.pas there is a variable WebModuleClass, which is a descendant of TWebModule. This WebModuleClass object has a public property Response of type TWebResponse, which gives you ability to set ContentType.
http://docwiki.embarcadero.com/Libraries/Berlin/en/Web.HTTPApp.TWebResponse.ContentType

Jmeter does not send JSON data in POST

I am trying to do a POST in jmeter with a json as the body data but I just get a 400 back. The URL I am sending to only accepts json. I have used the same curl in Postman and it worked just fine. I have tried putting the json in the parameters as a value with no name and that didnt work.
I am just trying to send {"uid":"jmtest","name":"newdevice"}.
Sorry I cant post more links.
The sampler result is
Thread Name: QA test 1-1
Sample Start: 2016-05-11 11:50:19 MDT
Load time: 86
Connect Time: 51
Latency: 86
Size in bytes: 282
Headers size in bytes: 244
Body size in bytes: 38
Sample Count: 1
Error Count: 1
Data type ("text"|"bin"|""): text
Response code: 400
Response message: Bad Request
Response headers:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 11 May 2016 17:50:19 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 38
Connection: keep-alive
X-Request-Id: 88339ee9-b74f-4e22-b581-e3124949d067
X-Runtime: 0.030877
HTTPSampleResult fields:
ContentType: application/json; charset=utf-8
DataEncoding: utf-8
And the request looks like
/url redacted
POST data:
{"devices":{"uid":"jmtest","name":"newdevice"}
[no cookies]
Request Headers:
Connection: keep-alive
Accept: application/vnd.moneydesktop.v2+json
Content_Type: application/json
MD-SESSION-TOKEN: redacted
Content-Type: application/x-www-form-urlencoded
Content-Length: 46
Host: redacted
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_72)
The response data is
{
: "status":"400",
: "error":"Bad Request"
}
To send a POST HTTP Request with the JSON Data inside the body, need to add,
HTTP Header Manger into your request and set the name as 'content-type' and value as 'application/json' this will attached into HTTP request header and what ever the data inside your request body will send as json format.
Image 1:Set HTTP Header Manager,
Image 2:Set HTTP Request Body Data
Make sure your HTTP Header Manager is configured to send Content-Type header with the value of application/json
Given you have samplers like "home page" and "login" it might be the case you're missing HTTP Cookie Manager
The most straightforward way to see what's wrong is to capture requests sent by Postman (whatever it is) and JMeter by a sniffer tool like Wireshark, identify the differences and configure JMeter accordingly.
Finally, it looks like you're using some developer snapshot so approach to pass JSON payload as 1st argument without name might not work. Try switching to "Body Data" instead.
If above steps won't help update your question with screenshots of View Results Tree listener (all 3 tabs) and Postman
Check out Testing SOAP/REST Web Services Using JMeter for more tips.
This solution from Dmitri T, really worked for me.
Make sure your HTTP Header Manager is configured to send "Content-Type" header with the value of "application/json".
I have faced the same issue and it was resolved by setting the value of Content Encoding to utf-8 in http request. Please try.
First thing you might wanna do is put the payload inside "Body Data" Instead of "Parameters"
Then, add a config element "HTTP Header Manager"
And add a parameter "content-type" With corresponding value "application/json"
Now hit. Should do!
I figured out my problem. I had set Content_type instead of Content-Type so it was creating two content type headers.
Though Hasiya explain really well but missed how to find http request.
If you chose http default will not see method option .
I had given HTTP/1.1 415 Unsupported Media Type
After deleting space before of HTTP Header Manager ,It got 200 and worked like a charm
You must use body data, not parameters. cut and paste it the next tab on your request. Also you should change implementation to Java and check if your token is valid. Finally check your results from view results tree reporting item. It must be work for you.
In addition to many said above, make sure that the copy/paste of Accept, Content-Type from website doesn't have a trailing space! It costs me many hours to figure it out. Just a caution.
Just edit to add that if that mistake happens, return code is 415: Unsupported Media Type
Insert your json in body data field.
In your HTTP Header Manager set the headers stores to:
"name" : Content-Type
"value": application/json
Yes, we need to pass the Content-Type header.
I faced the same issue and it took nearly a day to figure out until I came across this link - https://www.blazemeter.com/blog/performance-test-web-services
Header:
Name: Content-Type Value: application/json;charset=utf-8
If none of the abovementioned solutions work for you (as it was for me) take a second look at the HTTP Header Manager. I had empty header line there
Removing it solved the problem
I was facing the similar issue, the size of my request body was also huge so along with updating content-type as application/json, i gave content encoding as utf-8 and it worked perfectly fine.
As per your attached pic , You have pasted the request in queryparameters , the payload must be in body part
check the screenshot you have payload in queryParam section
Setting the HTTP header manager fixed the issue for me
HTTP Header Manager:
Name: Content-type and Value: application/json has resolved issue.

Is it possible for wcf data services to return metadata in json format?

Is it possible for wcf data services to return metadata in json format?
The server return error 415 from this request ->
GET http://services.odata.org/V3/(S(plcxuejnllfvrrecpvqbehxz))/OData/OData.svc/$metadata HTTP/1.1
DataServiceVersion: 3.0;
Host: services.odata.org
Content-Length: 0
Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
There's nothing preventing the service from offering such capability, but OData currently doesn't define JSON format for the metadata endpoint response. So the server would have to come up with one (proprietary). This holds true for WCF Data Services, I don't know enough about plain WCF Services (and if there's a JSON version of WSDL).
WCF can return json from methods, but service metadata is always xml (wsdl).

Creating requests in fiddler2 gives a 401 unauthorized but the 200 negotiated request doesn't follow

I'm trying to use fiddler to generate requests to receive a JSON response. I drag a request that was captured from IE with a 200 response into the request builder then change the Accept header to "Accept: application/json".
After that I execute the request and get a 401 unauthorized response but the negotiation with a following 200 response does not take place. If there another way I can get this to work?
The reason I was heading down this path was to track down why my AJAX requests against the SharePoint 2010 WCF data services were not returning data while using the .getJSON method in jQuery. Turns out the SP2010 WCF data services escape single quotes in the JSON response.
Single quotes should not be escaped according to the JSON spec.