microsoft graph api - error responses returning as plain text - asp.net-core

I'm using the the graph api in an internal .Net Core 3.1 webapi. However, each time a failed response is returned, it returns as a 500 internal error with the response as content-type: text/plain.
Status Code: NotFound
Microsoft.Graph.ServiceException: Code: Request_ResourceNotFound
Message: Resource '********' does not exist or one of its queried reference-property objects are not present.
Inner error:
AdditionalData:
date: 2020-10-13T13:44:21
request-id: 8053255b-2f1c-4543-8de7-*******
client-request-id: 8053255b-2f1c-4543-8de7-*******
ClientRequestId: 8053255b-2f1c-4543-8de7-*******
I am looking for the response to be return in JSON format similarly to the MS Graph Api explorer with the appropriate error code instead of a 500 Internal Error.

I believe the error you're looking for is inside
Microsoft.Graph.ServiceException -> Error -> Code.
If you'd like it in Json format you could convert it using JsonConverter.

Related

Custom error status code with gqlgen + go gin

Recently I have been updating my GO REST APIs into graphQl API's and I came across issue where I am unable to customise my status code with gqlgen.
Response I got
Headers
Status Code: 200 OK
{
data: null,
errors: [
{message: "Unauthorized access", path: ["..."]}
]
}
Expected Header
Status Code: 401 UNAUTHORISED
Any help would be really appreciating!
Assume you have a gqlgen resolver similar to this:
func (r *queryResolver) SecretItems(ctx context.Context, userID string,
password string) ([]SecretItems, error) {
// ...
if !isAuthorized(userID, password) {
return nil, errors.New("Unauthorized access")
}
// ...
}
then the described behavior is expected. Errors should be returned as part of
the response body.
GraphQL is transport agnostic. While it is often served over HTTP, it might be
served over other client-server Protocols as well. Handling errors in the
response body requires no assumptions about the protocol. Hence, you shouldn't
rely on HTTP status codes.
Handling errors in the response body has another advantage: Assume a request
contains multiple queries. Some of them succeed, some of them fail. Then the
response can contain the result of successful queries under data and errors
related to failed queries under errors.
References:
GraphQL website
Specification: Response
Hasura: GraphQL vs REST
Possible reason why you expected a 401 status code
The gqlgen docs on
authentication contain an example
where 401 status code is returned.
Why? This happens in a http handler used as middleware on the chi http server.
The 401 status code is not returned by a GraphQL resolver.

Invalid request body error when sending json string as data to an external api using CL_HTTP_CLIENT

We are facing an issue while sending json data to an external api using CL_HTTP_CLIENT.
The JSON data is produced using '/ui2/cl_json=>serialize( data = ls_body compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case )' .
when sending this JSON as data the the external api returns status 400 with response as
{ "errorCode": "INVALID_REQUEST_BODY", "message": "The request body is missing or improperly formatted. Unexpected character encountered while parsing value: \u001f. Path '', line 0, position 0." } .
we also stringyfied this JSON Data in backend as it might be due to parsing error but it didnt work.
The same stringyfied data tried to send through browser console using ajax and it did worked without any issue.
could any any one tell us how to handle this json object and send this to external api using CL_HTTP_CLIENT.
Note : JSON STRING is deeply nested .
Thanks in advance..
You can use request catcher service for getting SAP output.
Then check your output has valid json.
Check external api with rest tool like postman or SoapUI. Every developer not track guidliness may be external api has limitations.
The issue was with the unicodes in the string.
these were not accepted by the external api so removed from the string and sent to api and it did worked.
Thanks for You suggestion.

Karate API: JSON parsing error shown instead of wrong HTTP code returned

Given have the following list of steps
Given path 'verify'
And header x-api-key = apiKey
And header tenant-id = tenantId
And request a_json_object_with_invalid_user_id_to_verify
When method put
Then status 404
When the endpoint returned HTTP 200 (which is a bug that i need to fix) but with an invalid json response such as
{
"score" :
}
This exception is thrown
com.jayway.jsonpath.InvalidJsonException: net.minidev.json.parser.ParseException: Unexpected character (}) at position 15.
at com.jayway.jsonpath.spi.json.JsonSmartJsonProvider.parse(JsonSmartJsonProvider.java:64)
at com.jayway.jsonpath.internal.JsonContext.parse(JsonContext.java:82)
at com.jayway.jsonpath.JsonPath.parse(JsonPath.java:596)
at com.intuit.karate.JsonUtils.toJsonDoc(JsonUtils.java:84)
at com.intuit.karate.StepDefs.method(StepDefs.java:344)
I expect to see the test failed because of wrong status code 200 received of the expected 404. I would like to know if throwing this exception is correct in karate?
Thanks.
I apologize for mis-reading your question.
There is indeed a bug in Karate when handling malformed JSON responses.
We have opened an issue, you should be able to get a patch version very soon: https://github.com/intuit/karate/issues/259

Outpan API C# POST Request

What does the Outpan server expect when posting data?
GET was giving me a json info that I was able to convert to an object.
Now when I'm giving information back just like the name:
https://api.outpan.com/v2/products/UPCCode/name?apikey=xxx
it will always not understand my information (error 400). Already tried sending the json back with additional info, "Foodname" and "name = Foodname"

Need To Resolve Yelp API Response Error: “Signature is invalid”

My issue is when I try and fetch the Yelp API I keep getting this error time after time and i'm not sure why i've checked all the parameters and still cant get anything back from Yelp
My Yelp API Fetch Link -
https://api.yelp.com/v2/search?term=food&ll=37.788022,-122.399797&oauth_consumer_key=MY_Consumer_Key&oauth_token=MY_TOKEN&oauth_signature_method=HMAC-SHA1&oauth_signature=TOKEN_SECRET&oauth_timestamp=1456337018&oauth_nonce=1456337018
My Error -
error: {
text: "Signature was invalid",
id: "INVALID_SIGNATURE",
description: "Invalid signature. Expected signature base string:
GET&https%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch&ll%3D37.788022%252C-122.399797%26oauth_consumer_key%3DG02O6tXnFkSbsGYOJxCQDw%26oauth_nonce%3D1456337018%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1456337018%26oauth_token%3DBPMpqKrD4dA4xGGHKtC_-xitzb5LRNn8%26term%3Dfood"
}
Error in your API call url,yelp using oauth 1.0a,Which one url parameters should be in alphabet order(this one is telling clearly in your error description), So API call url should be like this:
https://api.yelp.com/v2/search?ll=37.788022,-122.399797&oauth_consumer_key=MY_Consumer_Key&oauth_nonce=1456337018&oauth_signature=TOKEN_SECRET&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1456337018&oauth_token=MY_TOKEN&term=food