0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

it worked when i sent parameters using the url.. now i just need to convert this to httppost and take parameters with json..
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.