Posting XML string to WebAPI through Swagger - asp.net-web-api

I have a web api method like this below "ProcessFeed".
I am using Swagger API to test this service.
The input Data needs to be a big XML string. The problem is where there is a double quote (") in the string, it is not working.
How can resolve this.
I tried making the method like this too - ProcessFeed(string data)
Code
public class InputDataModel
{
public string Data { get; set; }
}
public HttpResponseMessage ProcessFeed(InputDataModel inputDataModel)
{
var response = _processorCore.ProcessFeed(inputDataModel.Data);
}
Swagger
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"Data": \
"<Date>"2013-02-05"</Date> \
<Time>19:32:33.407</Time>" \
}' 'http://localhost:50545/processfeed'

Your problem is in the headers. You need to pass the Content-Type as application/xml

Related

Spring Boot - Json RequestBody, String/Enum with/without quotation marks

I went into a strange problem. Why does the first code accept input without quotation marks, but the second does not?
To be honest, the second one makes sense. But why is the first one accepting the input given without quotation marks?
I really would like to know why this decision was taken.
package com.example.corntest;
import lombok.extern.log4j.Log4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
#SpringBootApplication
#RestController
public class CornTestApplication {
public static void main(String[] args) {
SpringApplication.run(CornTestApplication.class, args);
}
//works - does NOT remove quotation marks
//curl 'http://localhost:8080/test' -X POST -H 'Content-Type: application/json' --data-raw '"SunGoesUp"' -vv
//works - but doesnt make sense - in comp. to code made in the bottom
//curl 'http://localhost:8080/test' -X POST -H 'Content-Type: application/json' --data-raw 'SunGoesUp' -vv
#PostMapping(value = "/test", consumes = APPLICATION_JSON_VALUE)
void mytestPost(#RequestBody String myString){
System.out.println(myString);
}
enum Test {
TESTA,
TESTB,
TESTC
}
//works
//curl 'http://localhost:8080/testEnum' -X POST -H 'Content-Type: application/json' --data-raw '"TESTA"' -vv
//does not work
//curl 'http://localhost:8080/testEnum' -X POST -H 'Content-Type: application/json' --data-raw 'TESTA' -vv
//Why
#PostMapping(value = "/testEnum", consumes = APPLICATION_JSON_VALUE)
void myTestEnum(#RequestBody Test myEnumValue){
System.out.println(myEnumValue);
}
}
In the case where you are using #RequestBody String myString the payload of the http request is put in there as-is. So whatever you send will be placed in there. The content-type doesn't matter as it will copy the payload of the request as is.
In the second case, an actual JSON string is required to be able to convert to an enum. A JSON based String needs to have quotation marks else it isn't a JSON String. Something other than a string cannot be converted to an enum.
To convert from the payload of the body to the requested object Spring uses an [HttpMessageConverter]. It will select the correct HttpMessageConverter based on the Content-Type header. In your case, assuming the defaults, this will result in the MappingJackson2HttpMessageConverter. Which will use the body to convert to the enum. Which will then fail as it isn't a valid JSON string (no quotation marks).
What is a String in JSON is explained here.

Graph API List Channel Messages Returnng UnknownError after Protected API Request Approved

I am unable to get a good response, and having hard time finding Docs. Can't find a way to lookup the inner error code at all either. I get a token using
curl -L -X POST 'https://login.microsoftonline.com/51bcb6e4-b738-4e87-a795-768e3f13b94f/oauth2/v2.0/token' \ -H 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'client_id=5bbd5786-31d3-4d84-a8d0-a0dac6522f93' \ --data-urlencode 'scope=https://graph.microsoft.com/.default' \ --data-urlencode 'client_secret=<secret>' \ --data-urlencode 'grant_type=client_credentials'
and then take the access_token value, and do this
curl -L -X GET 'https://graph.microsoft.com/v1.0/teams/d89133ea-bf32-4957-8300-aa5e083f45c1/channels/19:5b075a993aee4b3fa8f23be2cbb3b514#thread.skype/messages' \ -H 'Authorization: Bearer <token value from above>'
But I get this error 403:
{ "error": { "code": "UnknownError", "message": "", "innerError": { "date": "2021-03-09T19:15:23", "request-id": "c9a8cf3a-b914-44a8-a66a-506634f09d77", "client-request-id": "c9a8cf3a-b914-44a8-a66a-506634f09d77" } } }
how can I troubleshoot? the url for the GET seems right, based on all my research. we are using resource specific consent permissions, but the app is installed in the channel that I am trying to list messages. from
This question will be tracked from here.

How do I map the JSON body of a GET request to a parameter?

I have the following definition in my endpoint:
params do
requires :entities do
requires :id, type: String
optional :email, type: String
optional :phone, type: String
end
end
post "test" do
end
Note this only works as a POST request and the following CURL will work:
curl -XPOST localhost/test
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "entities": [{ "id": 3, "email": "test#abc.com" }] }'
However, the moment I change the declaration to get "test" instead of post, the corresponding curl request with -XGET no longer works due to the following validation error:
{"error":"entities is missing"}
If I remove the params requirements I can manually inspect the params hash at runtime and see that it only has a single key "route_info"
I'm currently using Grape 0.7.0
It happens because by specifying -d option you pass parameters in the body of the request while your endpoint is expecting them in the path as part of the url. Check here why it's a bad idea to pass body parameters in a GET request.
However, can use that option but if combination with -G.
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request
instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
So that your get request by using -d would look like:
curl -XGET -G localhost/test
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "entities": [{ "id": 3, "email": "test#abc.com" }] }'

Android Retrofit ParseSdk Geopoint

I have some place data in parse database.
I would like to use retrofit to find closet place to given lat and longitude
Below is the link , and curl request.
https://parse.com/docs/rest/guide#geopoints-geo-queries
curl -X GET \
-H "X-Parse-Application-Id: xxxxxx" \
-H "X-Parse-REST-API-Key: xxxxx" \
-G \
--data-urlencode 'limit=10' \
--data-urlencode 'where={
"location": {
"$nearSphere": {
"__type": "GeoPoint",
"latitude": 30.0,
"longitude": -20.0
}
}
}' \
How can i convert above curl to retrofit call ?
I think i have to use QueryMap somewhere , but cannot figure out where.
This is what i have so far.
#GET("/classes/Place")
void getPlaces(#Query("limit") Integer limit,
Callback<PlacesResult> callback);
This is request with headers "X-Parse-Application-Id: xxxxxx", "X-Parse-REST-API-Key: xxxxx" and form url encoded body with #Query("where") that takes up string.
You can put these headers in http call Interceptor for all requests.
Yes, this can be done with QueryMap, which will be of two query params.

Can a pointer field, to "user" class, be updated via rest api?

I have a class A that has a pointer (pointer field) to the special class User. Is it possible to automatically update the pointer field with the authenticated user, using the Rest API instead of having to call the API like the example bellow?
curl -X POST \
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-H "X-Parse-Session-Token: ${SESSION_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"user":{ "__type": "Pointer", "className": "_User", "objectId": "${id}"}}' \
https://api.parse.com/1/classes/A
I would like to update the User field by just using the call:
curl -X POST \
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
-H "X-Parse-Session-Token: ${SESSION_TOKEN}" \
-H "Content-Type: application/json" \
https://api.parse.com/1/classes/A
And an A entry would be created.
A : {Pointer<_User> user_from_session, string objectId}
Any enlightenment is appreciated.
Cheers.
I've managed to accomplish what I wanted by adding an afterSave trigger. When creating A, with an authenticated request, I will read the current user from the session and set the field in the class. Bellow the example.
Parse.Cloud.afterSave("A", function (request, response) {
var record = request.object;
var user = Parse.User.current();
if (record.existed() == false) {
record.set("user", user);
record.save().
then(function (record) {
response.success();
}, function (error) {
response.error();
});
} else {
console.log("[DEBUG] Normal save (A).");
response.success();
}
});
Cheers.

Resources