Hello,
I am trying to get HttpApi working on this ServiceMethod:
[ServiceMethod]
[ApiMethod(HttpApiMethod = ApiRequestMethod.Post,
HttpApiPath = “action/executeaction”,
HttpApiOperationId = “ExecuteAction”)]
public string ExecuteAction(string jsonData)
{
}
It is called like so: http://localhost:8099/api/action/executeaction
I am using JMeter to test and the ExecuteMethod is getting hit but the jsonData parameter is null
There are no Uri parameters and the json string is sent via request body.
How should this method be defined?
Thanks,
Thom
antonk
(antonk)
September 22, 2020, 9:55am
2
Hello
Please double-check request content type. It has to be application/json
Request body should look like
{
"jsonData": "AAA"
}
You can pass parameter via URL as well:
http://localhost:8099/api/action/executeaction?jsonData=A1A2
Regards
Hi Anton,
So I tried this and jsonData is still null on the server:
{ “jsonData”: {“ActionId”: 201, “ClientAppView”: 1, “IsFirstPass”: true, “Language”: “en-US” } }
// Thom
Here is the Swagger Curl output:
curl -X POST “http://localhost:8099/api/action/executeaction ” -H “accept: application/json” -H “Content-Type: application/json” -d “{“jsonData”: {“ActionId”: 201, “ClientAppView”: 1, “IsFirstPass”: true, “Language”: “en-US” }}”
// Thom
antonk
(antonk)
September 22, 2020, 11:02am
5
You are trying to pass an object to a parameter that expects a String value.
Can you show me what the correct way to send a json string to the server using request body is?
// Thom
antonk
(antonk)
September 22, 2020, 11:13am
7
You need to escape the JSON string. F.e. for your sample it would look like
{"jsonData": "{\"ActionId\": 201, \"ClientAppView\": 1, \"IsFirstPass\": true, \"Language\": \"en-US\" }"}
Server receives this string as
{"ActionId": 201, "ClientAppView": 1, "IsFirstPass": true, "Language": "en-US" }
I’ve tested this approach using Postman and your method definition and it seem to work fine.
Regards
Success! Works in both Swagger and JMeter.
Thank you very much for help, Anton.
// Thom
1 Like
Hello again, Anton.
One thing I notice using HttpApi compared to my RealThinClient server is that the returned JSON string is enclosed in double quotes.
I am used to it coming over like this with RTK {“Result”: true} but with HttpApi like this: “{“Result”: true}”
Is there something I can tweak in the response to remove these double quotes?
// Thom
Changing the return to Binary and returning the jsonResult like this did the trick:
public Binary ExecuteAction(string jsonData)
{
return new ApiResult(HttpStatusCode.OK, ApiResultContentType.Json, jsonResult);
}
// Thom
antonk
(antonk)
September 22, 2020, 2:58pm
13
Not only there are quotes. There are also escape sequences in the string itself: "{\"Result\": true}"
You are right. If you want to bypass the serializer and return something AS IS, you need to use the ApiResult
class. Note that this class also allows you to return custom HTTP response codes.