I am using Azure Functions
and I have a third party API
I want to call. I have the URL to it but it is a POST
method so I know to use a "post" for method type
and also I know about the that I can create a class
and pass the values in there as a class object
.
But what if the BODY sample they need is more complex. In my case something like this below.
In that case do I still create a class
? How should I model it when it more complex like this?
{
"familyHierarchy": {
"familyMembers": {
"memberIds": ["string"],
"age": 0,
"marriageDate": "string"
},
"familyGroup": "string"
},
"pageSize": 0
}
Also here is what currently I have for my method signature but not sure about FromBody
that I am asking about
[FunctionName("GetFamilyInfo")]
public static async Task<IActionResult> GetFamilyInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = "families/")] HttpRequest req, ILogger log)
2
Answers
Have you considered creating a data class for that request body?
When you declare the data class and assign its values, you can use the Gson library to convert it to JSON and send that as the request body.
Hope this helps!
As mentioned in the comments, you don’t really need to go through the process of deserializing the request body in a class and then serialize it again to send as request body to your API.
Here’s the sample code (untested though). It basically reads the request body and passes it to the post request to the 3rd party API. Hopefully it will give you some ideas about how to proceed: