user3096746
user3096746

Reputation: 61

HttpPost how to receive parameters c#

I have a method on web service and I need to receive parameters with [HttpPost].
I am new at this, and I really don't know how.
I need to receive the following:

**** long requestId, string text, byte[] audio, short languageId****

    public void AddAnswer (long requestId, string text, byte[] audio, short languageId) 
    {
        string userIdWhoAnswers = (User as TokenPrincipal).userId.ToString();

        long userId = Convert.ToInt64(userIdWhoAnswers);

        using (var context = new WordsEntities())
        {
            Answers answer = new Answers();
            answer.requestId = requestId;
            answer.userId = 10;
            answer.text = text;

            answer.audioExtension = audio;
            DateTime datee = DateTime.Now;
            answer.timePosted = datee;
            answer.languageId = languageId;

            context.Answers.Add(answer);
            context.SaveChanges();
        }
    }

This is my method but with HttpGet, I need to convert it to HttpPost. Can someone please help me?

Upvotes: 0

Views: 2071

Answers (2)

DhanashriJoshi
DhanashriJoshi

Reputation: 21

you can refer to this question and specially this link

TLDR; FromBody accepts just one parameter; try passing a JSON string with all required inputs in a single parameter to your HttpPost.

Upvotes: 0

imperugo
imperugo

Reputation: 385

Looking your code I think you are sending the information using the body and not the url (in fact you are trying to send a byte array that usually isn't compatible with the query string).

For this reason you have to use the FromBody attribute near to the parameter name

public void AddAnswer ([FromBody] long requestId, [FromBody] string text, [FromBody] byte[] audio, [FromBody] short languageId)

moreover I think that the byte[] doesn't work. Probably you have to work with multipart

Upvotes: 1

Related Questions