Shahnaz Raheem
Shahnaz Raheem

Reputation: 228

Flutter send data by put request

I need to put some data thorugh api here is my code

  static voteplusQuestion(data, int flag){
    print('Api');
    print(data);
    if( flag == 1 ){
      var updateQuestionData = {'Would': data.would, 'Rather': data.rather, 'wouldClick': data.wouldClick, 'ratherClick': data.ratherClick + 1, 'QuestionID': data.QuestionID,};
      print(updateQuestionData);
    }
    if( flag == 0){
      var updateQuestionData = {'Would': data.would, 'Rather': data.rather, 'wouldClick': data.wouldClick  + 1, 'ratherClick': data.ratherClick, 'QuestionID': data.QuestionID,};
      print(updateQuestionData);
    }
    return http.put(
      'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather',
      headers: <String, String>{
        'Content-Type': 'application/json',
      },

    );
  }

I need to put updateQuestionData to API but i am not sure how can i do this. Also the print output of updateQuestionData is like this

{Would: Shaving Cream, Rather: Soap, wouldClick: 15, ratherClick: 13, QuestionID: 16563fa7-e833-445f-a76b-a9fbaab3a301}

And the Api is working in code pen like this

var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather');
xhr.onreadystatechange = function(event) {
  console.log(event);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');

xhr.send(JSON.stringify({Would: "Coffe",Rather: "Tea", wouldClick: 15, ratherClick: 13, QuestionID: "16563fa7-e833-445f-a76b-a9fbaab3a301"}));

I have set the header in function but now dont know how can i set the body

Upvotes: 0

Views: 435

Answers (1)

Seddiq Sorush
Seddiq Sorush

Reputation: 3132

You can send data in a put method as follow:

 static voteplusQuestion(data, int flag) async{
   var updateQuestionData;

   if( flag == 1 ){
      updateQuestionData = {'Would': data.would, 'Rather': 
      data.rather, 'wouldClick': data.wouldClick, 'ratherClick': 
      data.ratherClick + 1, 'QuestionID': data.QuestionID,};
   }

  if( flag == 0){
   updateQuestionData = {'Would': data.would, 'Rather': data.rather, 
    'wouldClick': data.wouldClick  + 1, 'ratherClick': 
    data.ratherClick, 'QuestionID': data.QuestionID,};

   }

    String url = 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather';
    Map<String, String> headers = {"Content-type": "application/json"};
    String data = jsonEncode(updateQuestionData);
    // make PUT request
    Response response = await put(url, headers: headers, body: data);
    // check the status code for the result
    int statusCode = response.statusCode;
    print(statusCode);
    // this API passes back the updated item with the id added
    String body = response.body;
    print(body);

   return body;
 }

I am not sure about your use case, but I believe this could be implemented much cleaner.

Note: to use put method or to perform the update operation you need to add the id of the item, I am sure about the url existed in this example

Upvotes: 1

Related Questions