Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 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 Reply

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);
              });
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...