首页 > 其他分享 >HTTP 请求体编码用 json 还是 x-www-form-urlencoded

HTTP 请求体编码用 json 还是 x-www-form-urlencoded

时间:2024-01-25 21:37:11浏览次数:31  
标签:www HTTP form application json urlencoded POST

  • application/x-www-form-urlencoded
  • application/json

application/json 对初学者友好
application/x-www-form-urlencoded 对 Postman 友好

axios 和 superagent 默认使用 JSON body

来自专家的建议

The Stripe API is organized around REST. Our API has predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs

-- Stripe Docs

Creating or updating a resource involves performing an HTTP PUT or HTTP POST to a resource URI. In the PUT or POST, you represent the properties of the object you wish to update as form urlencoded key/value pairs. Don't worry, this is already the way browsers encode POSTs by default. But be sure to set the HTTP Content-Type header to "application/x-www-form-urlencoded" for your requests if you are writing your own client.

-- Twilio Docs

一些重要的事项

json 会造成预检请求问题(CORS)
Preflight OPTIONS requests are always sent with JSON

URL 编码数组是一个麻烦事

URL ENCODE: partners[]=Apple&partners[]=Microsoft&partners[]=Activision
JSON ENCODE: {"partners":["Apple","Microsoft","Activision"]}

一名开发者的回复

Ali Sherief

•[20年7月5日 • Edited on 7月5日](https://dev.to/bcanseco/request-body-encoding-json-x-www-form-urlencoded-ad9#comment-11d5i)

My app was using application/json to make requests to my API but I found a downside to it. The preflighted OPTIONS request is sent in order, but the actual POST/GET/whatever request is sent arbitrarily later after the next requests have been made. So this wrecks a stateful API if you send a POST with application/json before navigating to another page and GETing the same application/json there because the OPTIONS request for the POST will be sent first, then the next GET, then the POST itself.

For this reason I try to use application/x-www-form-urlencoded as much as possible. Preflighted requests can make race conditions.

我的应用使用 application/json ,但是发现了一些问题,预检请求按顺序发送,但是实际的 POST/GET/... 请求会任意的顺序发送

标签:www,HTTP,form,application,json,urlencoded,POST
From: https://www.cnblogs.com/ninkaki/p/17988211

相关文章

  • 解决 fatal: unable to access 'https://github.com/alibaba/nacos.git/': Failed to
    直接打开这个网站:https://sites.ipaddress.com/github.com/。找到网站中的IP地址复制出里面的IPAddress并粘贴到hosts里面。以下是macos上使用命令行打开方式,也可以直接在硬盘上找到这个文件打开sudovim/private/etc/hosts在最后一行添加如下代码140.82.113.4githu......
  • APISIX同时代理websocket和http请求 websocket无法正常工作
    1.配置了一个路由同时代理websocket和http请求 2.打开了websocket支持,websocket还是无法工作  3.定位3.1通过postman分别走apisix和不经过apisix建立websocet连接,发现经过apisix也能成功建立连接,但是10秒左右就自己断开了。  3.2思考3.3决定试一下是否和超时配置......
  • nginx 如何强制跳转 https
    本项目nginx作为代理服务项目上线,客户说要加个安全证书,于是安全证书是加上了,可是htttp和https都能访问网站,客户要求不行必须强制用带有https的地址访问开整这是http和https都能访问的nginx.conf 关键配置  server{listen80;lis......
  • Java中SimpleDateFormat时YYYY与yyyy以及HH和hh的区别注意踩坑
    场景Java开发手册中为什么要求SimpleDateFormat时用y表示年,而不能用Y:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/131052335在使用SimpleDateFormat在获取当前日期时因使用了YYYY导致20231231这个日期被格式化为20241231这里推荐在日期处理时统一使用封装工具......
  • A Format Compliant Encryption Method for 3D Objects Allowing Hierarchical Decryp
    Abstract—Withtheincreasingpopularityof3Dobjectsinindustryandeverydaylife,3Dobjectsecurityhasbecomeessential.Whilethereexistsmethodsfor3Dselectiveencryption,whereaclear3Dobjectisencryptedsothattheresulthasthedesiredl......
  • HTTP连接池在Java中的应用:轻松应对网络拥堵
    网络拥堵是现代生活中无法避免的问题,尤其是在我们这个“点点点”时代,网页加载速度直接影响到我们的心情。此时,我们需要一位“救世主”——HTTP连接池。今天,就让我们一起探讨一下,这位“救世主”如何在Java中大显神通。首先,我们要明白,什么是HTTP连接池?简单来说,它就像一个“连接银行”......
  • 使用Java中的OkHttp库进行HTTP通信:快速、简单且高效
    在Java的世界里,进行HTTP通信的方式多种多样。其中,OkHttp以其简单、高效和强大的功能受到了开发者的广泛欢迎。今天,我们就来深入探讨如何使用OkHttp库在Java中进行HTTP通信。首先,OkHttp是一个基于HTTP/2和SPDY的客户端,提供了现代且高效的通信方式。它不仅支持同步请求和异步请求,还提......
  • 使用Go语言编写高效的HTTP代理服务器:轻松应对流量洪流
    在这个网络时代,HTTP代理服务器成了我们与世界沟通的“桥梁”。它们能帮我们在浏览网页、下载文件时绕过某些限制,也能让我们在测试网络应用时隐藏真实IP。那么,如何用Go语言编写一个高效的HTTP代理服务器呢?让我们一起探讨这个问题,轻松应对流量洪流!首先,让我们来了解一下什么是HTTP代理......
  • Go语言实现HTTP代理的原理与步骤
    在计算机网络中,代理服务器是一种重要的中间件,它能够帮助客户端和服务器进行通信,并处理各种网络请求和响应。在Go语言中,我们可以使用标准库中的"net/http"包来实现一个简单的HTTP代理服务器。下面我们将介绍实现HTTP代理的原理与步骤。一、代理服务器的工作原理代理服务器的工作原理......
  • 探讨Go语言中的HTTP代理模式:看Go如何玩转网络中转站
    在互联网的海洋中,HTTP代理服务器像一座灯塔,为我们的网络冲浪提供了指引。而当Go语言遇上HTTP代理,会碰撞出怎样的火花呢?今天,让我们一起探讨Go语言中的HTTP代理模式,看看它如何玩转这个网络中转站!首先,让我们来了解一下什么是HTTP代理模式。简而言之,HTTP代理模式就是通过一个代理服务器......