skip to Main Content

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


  1. Have you considered creating a data class for that request body?

    
    public class FamilyHierarchy {
    
       private FamilyMembers familyMembers;
       private String FamilyGroup;
       
       public FamilyHierarchy(FamilyMembers familyMembers, FamilyGroup familyGroup) {
          
          this.familyMembers = familyMembers;
          this.familyGroup = familyGroup;
    
    }
    
       private class FamilyMembers {
       
          private String[] memberIds;
          private int age;
          private String marriageDate;
    
       }
    
    }
    
    

    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.

    
    FamilyHierarchy familyHierarchy = new FamilyHierarchy()
    
    [... assign values here ...]
    
    Gson gson = new Gson();
    String requestBodyJson = gson.toJson(familyHierarchy);
    

    Hope this helps!

    Login or Signup to reply.
  2. 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:

    [FunctionName("GetFamilyInfo")]
    public static async Task<IActionResult> GetFamilyInfo([HttpTrigger(AuthorizationLevel.Function, "post", Route = "families/")] HttpRequest req, ILogger log)
    {
        string requestBody = String.Empty;
        using (StreamReader streamReader =  new  StreamReader(req.Body))
        {
            requestBody = await streamReader.ReadToEndAsync();
        }
        //Call the 3rd party API and send the request body
        HttpClient client = new HttpClient();
        await client.PostAsync("https://your3rdpartyapi.com", requestBody);
        ...do some more stuff if needed and return proper status code back
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search