api request problem response 500 from backend - api

All welcome, I ran into a problem in which at the time of passing id and body in the method it is received, it gives 500 errors. In swagger I have an infected method, it looks like this:
swagger
Accordingly, in the body of the request, I pass all the parameters of the fields it needs (I drive them in strictly, just to check for operability):
code
In response I get this:
response
If you try to do the same in the swagger, then the method works and a 200 response comes:
swagger 200 response
What am I doing wrong?
I tried to make the code asynchronous, I tried to pass fields in different ways, but nothing happens, the answer comes 500

Try to put the id field inside the obj.
const form = {
id: 790,
...
}
You are passing too much params inside the callback (id, form).

Related

Cypress, not able to intercept a call

I am trying to intercept following call, that gets executed after click on SAVE operation.
Its a POST 201 call.
Sample request URL is : https://www.test.com/abc/def/ghi/jkl/1234/mno/pqr/stu/9876
where 1234 and 9876 are dynamic changing parts of url.
But, it is not getting intercepted, and I am getting below error:
Timed out retrying after 2000ms: cy.wait() timed out waiting 2000ms for the 1st request to the route: submissionCall. No request ever occurred
My test code is as follow:
cy.intercept(‘**def/ghi/jkl/‘+’*’+’/mno/pqr/stu/*’).as('#submissionCall’)
cy.get("button[class='btn save’]”).click();
cy.wait('#submissionCall').then((result) => {
cy.log(response)
})
I have tried cy.intercept(‘**/mno/pqr/stu*’).as('callToIntercept’) but this also didnt work.
Can someone help me by pointing, what I am doing wrong and what are the other best possible ways of intercepting same?
Thanks.
In intercept method; you should change the url part like this:
cy.intercept("**/mno/pqr/stu/**").as('callToIntercept');
Also you should edit last part like this:
cy.wait("#callToIntercept").then(({ response }) => {
cy.log(response.body);
});
I hope that works!

Why is axios returning a different responseURL than the request url?

I am making an Axios GET request from my React Native app to my locally served backend.
The response "config" object shows a "baseURL" of "http://192.168.1.68:8080/api" and a "url" of "inventory?status=ALL&barcode=127035838".
The response "request" object shows a "_url" of "http://192.168.1.68:8080/api/inventory?status=ALL&barcode=127035838".
However! the response "responseURL" attribute shows a value of "http://192.168.1.68:8080/api/inventory?status=ALL&barcode=127035838%1D".
The request from the app is not finding the barcode, returning 204. Making the same request from Postman, I am able to find the barcode with a 200 status. This makes me believe the issue is not on the backend. I switched from Axios to fetch and found the same result.
It is my guess the additional "%1D" on the end of the barcode is the cause of frustration. Why is the rsponseURL different from the request url. Why is axios adding "1%D", and how can I make it stop? Is there something else going on?
My solution:
let uriSN = serialNumber;
uriSN = encodeURIComponent(serialNumber);
uriSN = uriSN.replace('%1D', '');
Encoding the barcode and then stripping it of the "1%D" solves the issue. I do not know if it is the best or complete solution.
%1D is the url-encoded hex for 29 which is an ASCII control character called the "group seperator" so your serialNumber might have this character which is being parsed accordingly (by design) through the encodeURIComponent function. This is known as "GIGO" (garbage in garbage out).

Laravel 5.7 Handle email verification errors from API

I'm using laravel 5.7 to build an API REST.
When I add the verified middleware to my route group and I try to login with an unverified user by my client api, I get the error 400 Bad request. This error is too generic and don't show the problem clearly for my customer (it's happens in a login form).
If I to edit the render() method in Handler.php to ignore the isApiCall() and return parent::render($request, $e);, so I get the full error (Your email address is not verified, status 403), but when I try to return this in a json object with response()->json([$e]), the response is always empty.
So how to handle properly the api errors to be return the full message from exception, in this case?
You probably have already solved this problem, but this solution might help someone else:
The reason why your response was returning an empty array is because you were passing an Exception type object into the json function that is expecting an array. The response should look like this:
return response()->json(['message' => $e->getMessage()]);
This will return a json response like:
{
"message": "Your email address is not verified"
}

Cannot JSON.parse serverless event.body

I’ve been working on a simple function with the serverless framework that gets some data in a http POST, does some analysis and sends the results back. I got it working locally on my machine using serverless-offline but when it comes to deploying it I’m getting an error parsing the event.body.
Logging out the event.body it’s a string that looks like this:
----------------------------267253304929569989286258
Content-Disposition: form-data; name="text"
TEST
----------------------------267253304929569989286258--
so it makes sense that the parse is failing but I have no idea why this error it happening.
Any suggestions? I’ve tried a bunch of different things but am completely stumped.
Thanks in advance!
You can't JSON.parse that event.body cause it isn't JSON. It looks like whatever POSTed that data is using a multipart form POST style request rather than sending JSON. How are you invoking the HTTP POST?
I had the same issue and after a lot of debugging noticed 2 important things:
1.When the content type is application/x-www-form-urlencoded you might need to parse the data in a different way:
const qs = require('querystring');
module.exports.run = async event => {
try {
const data = qs.parse(event.body);
console.info('DATA:', data);
} catch(e) {
console.error(e.message);
}
}
2.When the Content-Type of the request is multipart/form-data the parsing will be even more complicated. I will suggest extra dependency to parse it like multiparty or any other of your choice
Thank you #Brian Winant! I am putting the answer here as a screenshot so it's clearer. In Postman, do the following:
AWS Lambda would return event.body as encoded query strings if the content-type is x-www-urlencoded. To have it return a JSON string you can then parse, send JSON data and set content-type as application/json.

How to log query string parameters with Postman

I'm using the tests of Postman to log into the console some of the details included in a JSON response.
The part of the test that logs is the following:
var data = JSON.parse(responseBody);
if (data.result[0] !== undefined) {
console.log(data.result[0].number, "|", data.result[0].category;
}
else console.log(QUERYSTRING_PARAMETER, "is not present");
I've tried many sintaxes/formats to have the value of the QUERYSTRING_PARAMETER passed to the test. However when data.result is empty with every sintax I've tried, the test simply logs QUERYSTRING_PARAMETER not defined. How can I pass the value from the query string parameters in the URL to the test to be evaluated/logged?
Thanks in advance
I'm not sure if it corresponds to what you need, but if you want data from your query, you may use the 'request' object (refer to the Postman sandbox, paragraph 'Request/response related properties').
You can get request method, url, headers and (maybe the one for you) data.
Alexandre