Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

javascript - Am I doing something wrong in this API call?

Here is the code:

 async createTemplate({ commit }, data) {
      console.log("CREATE TEMPLATE data STORE.JS: " + JSON.stringify(data));
      return await API.put(
                "asulink",
                "/scheduler/schedule",
                {
                  "schedule_name": "MJ Test 3",
                  "public": "true",
                  "local_times": [
                    1200,1230,1300,1330,1400,1430
                  ]
                }
              ).then((response) => {
                console.log("API.PUT ADD A SCHEDULE"+JSON.stringify(response))
              })
              .catch((err) => {
                window.console.log("Error occured", err);
              });
    },

If I make this API call I get a 400 error. The error says: Please send a valid JSON body. But if I copy the above JSON body and make the same PUT API call using OpenAPI I get a 200. I am not sure whats going on.

And this is not an axios call, but a Amplify API.put call.

question from:https://stackoverflow.com/questions/65887643/am-i-doing-something-wrong-in-this-api-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Documentation says something else. You need to send the request in body.

check out this link https://docs.amplify.aws/lib/restapi/update/q/platform/js#put-data

async createTemplate({ commit }, data) {
      console.log("CREATE TEMPLATE data STORE.JS: " + JSON.stringify(data));
      return await API.put(
                "asulink",
                "/scheduler/schedule",
                {"body": {
                  "schedule_name": "MJ Test 3",
                  "public": "true",
                  "local_times": [
                    1200,1230,1300,1330,1400,1430
                  ]
                }, "headers": {}}
              ).then((response) => {
                console.log("API.PUT ADD A SCHEDULE"+JSON.stringify(response))
              })
              .catch((err) => {
                window.console.log("Error occured", err);
              });
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...