我正在通过邮递员向swagger api发送请求,并发送一个body参数。问题是在api中我无法检索该参数。 req.body或req.swagger.params.body均未定义。实际上,我已经检查了请求对象的每个属性,并且正文不存在。
我的邮递员请求或摇摇欲坠的定义有问题吗?
邮递员中的请求
邮递员自动生成的http代码:
POST /myUrl/
Host: 127.0.0.1:10010
Content-Type: application/json
cache-control: no-cache
Postman-Token: xxxxx-xxxx-xxx-xxxx
{
"username": "user2",
"password": "pass2"
}------WebKitFormBoundary7MA4YWxkTrZu0gW--
邮递员自动生成的卷曲代码:
curl -X POST \
http://127.0.0.1:10010/users/ \
-H 'Content-Type: application/json' \
-H 'Postman-Token: f8c71ed0-6ef1-4dcf-b1b6-a7f2777a759e' \
-H 'cache-control: no-cache' \
-d '{
"username": "user2",
"password": "pass2"
}'
挥舞的端点定义:
swagger: "2.0"
info:
version: "0.0.1"
title: XXXXX
host: 127.0.0.1:10010
basePath: /
schemes:
- http
- https
consumes:
- application/json
produces:
- application/json
tags:
- name: xxx
description: Endpoints callable by the xxx
paths:
/users/:
x-swagger-router-controller: user.controller
post:
tags:
- xxx
summary: Create a new user
description: Create a new user
operationId: createUser
consumes:
- application/json
parameters:
- in: body
name: user
description: The user to create
schema:
type: object
required:
- username
- password
properties:
username:
type: string
password:
type: string
responses:
200:
description: Success
schema:
$ref: '#/definitions/CreateUserResponse'
答案 0 :(得分:1)
------WebKitFormBoundary7MA4YWxkTrZu0gW--
似乎是错误的。
您的Content-Type均为multipart/mixed; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW--
,并使用边界分隔表单编码参数。
或者您的Content-Type为application/json
,然后您发送不需要边界的json。
但是当您的挥霍消耗application/json
时,请确保从身体上移除------WebKitFormBoundary7MA4YWxkTrZu0gW--
。