From my JavaScript client, I send the JSON data using POST method
fetch("https://%$%$%%$.azurewebsites.net/api/JointPose?", {
method: "POST",
body: JSON.stringify({
JointPose1: `${Joint1Pose}`,
JointPose2: `${Joint2Pose}`,
JointPose3: `${Joint3Pose}`,
JointPose4: `${Joint4Pose}`,
JointPose5: `${Joint5Pose}`,
JointPose6: `${Joint6Pose}`,
JointPose7: `${Joint7Pose}`
}),
headers: {
"Content-Type": "application/json; charset=UTF-8",
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
I try to retrieve the request body using HTTPtrigger in my Azure Functions app
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "JointPose")] HttpRequest req)
{
MemoryStream ms = new MemoryStream();
await req.Body.CopyToAsync(ms);
string jointPoseData = ms.ToString();
//using StreamReader reader = new(req.Body);
//string bodyStr = await reader.ReadToEndAsync();
//var jointPoseData = JsonConvert.DeserializeObject<JointPoseData>(bodyStr);
return new OkObjectResult( new { Content = new StringContent(JsonConvert.SerializeObject(jointPoseData), Encoding.UTF8, "application/json") });
}
public class JointPoseData
{
public string JointPose1 { get; set; }
public string JointPose2 { get; set; }
public string JointPose3 { get; set; }
public string JointPose4 { get; set; }
public string JointPose5 { get; set; }
public string JointPose6 { get; set; }
public string JointPose7 { get; set; }
}
The response I get after opening the URL is
When opened the URL link in browser
Expected output is:
JointPose1: 0.31, JointPose2: 0.32,…….
I think the jointPoseData is null. How do I retrieve the request body with appropriate values?