错误:400错误请求使用提取将数据发布到本地服务器

问题描述

我正在使用访存将从表单收集的数据发布到本地服务器。问题是我收到“错误:400(错误请求)”,我也不知道为什么。服务器已启动并正常运行,我可以从中成功获取数据。

我使用提取方式有误吗?

#Create dup handles
proc dup(oldfd: FileHandle): FileHandle {.importc,header: "unistd.h".}
proc dup2(oldfd: FileHandle,newfd: FileHandle): cint {.importc,header: "unistd.h".}

# Dummy filename
let tmpFileName = "tmpFile.txt"

template captureStdout*(ident: untyped,body: untyped) =
  var stdout_fileno = stdout.getFileHandle()
  # Duplicate stoud_fileno
  var stdout_dupfd = dup(stdout_fileno)
  echo stdout_dupfd
  # Create a new file
  # You can use append strategy if you'd like
  var tmp_file: File = open(tmpFileName,fmWrite)
  # Get the FileHandle (the file descriptor) of your file
  var tmp_file_fd: FileHandle = tmp_file.getFileHandle()
  # dup2 tmp_file_fd to stdout_fileno -> writing to stdout_fileno Now writes to tmp_file
  discard dup2(tmp_file_fd,stdout_fileno)
  #
  body
  # Force flush
  tmp_file.flushFile()
  # Close tmp
  tmp_file.close()
  # Read tmp
  ident = readFile(tmpFileName)
  # Restore stdout
  discard dup2(stdout_dupfd,stdout_fileno)

proc main() =
  var msg = "hello"
  echo msg & "1"
  var s: string

  captureStdout(s):
    echo msg & "2"
    msg = "Ciao"

  echo msg & "3"
  echo ">> ",s
  assert s == "hello2\n"

when isMainModule:
  main()
  # Check it works twice
  main()
async function handleFetch(payload) {
    let url = 'http://localhost:3000/api/teddies/order';
    let options = {
        method: 'POST',headers: { 'Content-Type': 'application/json' },body: payload
    }
    response = await fetch(url,options);
    return (response);
}

我要发送的数据是带有en项列表的联系人对象(只是JSON.stringify并作为正文发送):

function handleForm(orderList) {
    const form = document.getElementById('order');

    form.addEventListener('submit',async function (e) {
        e.preventDefault();
        let contact = new Contact(form.surname.value,form.name.value,form.adress.value,form.city.value,form.mail.value);
        let order = JSON.stringify({ contact,orderList });
        const response = await handleFetch(order);
        const data = response.json();

        console.log(response);
    });
}

Chrome检查器提供的信息:

{"contact":{"surname":"Tom","name":"Bombadil","adress":"123 The Shire","city":"MiddleEarth","mail":"Tom.bomb@gmail.com"},"orderList":[{"colors":["Tan","Chocolate","Black","White"],"_id":"5be9c8541c9d440000665243","name":"norbert","price":2900,"imageUrl":"http://localhost:3000/images/teddy_1.jpg","description":"Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."},{"colors":["Pale brown","Dark brown","_id":"5beaa8bf1c9d440000a57d94","name":"Arnold","price":3900,"imageUrl":"http://localhost:3000/images/teddy_2.jpg",{"colors":["brown"],"_id":"5beaaa8f1c9d440000a57d95","name":"Lenny and Carl","price":5900,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.","imageUrl":"http://localhost:3000/images/teddy_3.jpg"}]}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)