Home > Back-end >  convert PHP POST script to C#
convert PHP POST script to C#

Time:10-28

I am trying to access a 3rd party API. The documentation of the 3rd party API provider has given the following PHP code on how to access the data sent. My code is written in C# dotnetcore. I have tried several ways to access the API but I get a Error 415. Can someone help me resolve this.

<?php

    $mmid         = $_POST['mm_id'];
    $ooid             = $_POST['oo_id'];

?>

My C# Code

    [HttpPost("b_notice")]
    [ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
    public async Task<ActionResult> APIUrl(string mm_id)
    {
        try
        {
            return Ok(mm_id);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }

The response code I get is 415 Unsupported Media Type. . I think they don;t use JSON. Can someone help me resolve this ?

Note: The 3rd party provider says they use 'application/x-www-form-urlencoded.

CodePudding user response:

You should be able to accomplish this by setting the Content-Type response header. The API is expecting input from an html form but it's getting input from somewhere it doesn't expect. I'm not sure how you would go about changing the headers in C# but that's a good place to start. Try testing it out in an http request debugger like postman

  • Related