简体   繁体   English

使用Postman的JSON请求

[英]JSON request using Postman

I am sending a raw Json requet using postman to an API service which feeds it to another web service and finally a database. 我正在使用邮递员将原始的Json requet发送到API服务,该服务将其馈送到另一个Web服务,最后是数据库。 I want to attach a file to the raw Json request. 我想将文件附加到原始Json请求。

I am attaching below the current request I am sending. 我在下面附上我正在发送的当前请求。 Is it the right way? 这是正确的方法吗? The first name and other information is going through but the attachment is not. 名字和其他信息正在审核中,但附件未审核。 Any suggestions? 有什么建议么?

{
  "Prefix": "",
  "FirstName": "test-resume-dlyon",
  "LastName": "test-dlyon-resume",
  "AddressLine1": "test2",
  "AddressLine2": "",
  "City": "Invalid Zipcode",
  "State": "GA",
  "Zip": "99999",
  "Phone": "9999999999",
  "Email": "testresumedlyon@gmail.com",
  "Source": "V",
  "WritingNumber": "",
  "AgeVerified": true,
  "AdditionalSource": "",
  "EnableInternetSource": true,
  "InternetSource": "",
  "ExternalResult": "",
  "PartnerID": "",
  "SubscriberID": "15584",
  "Languages": [
    "English",
    "Spanish"
  ],
  "fileName": "resume",
  "fileExtension": "docx",
  "fileData": "UELDMxE76DDKlagmIF5caEVHmJYFv2qF6DpmMSkVPxVdtJxgRYV"
}

There is no "correct" format to attach a file to a JSON. 没有将文件附加到JSON的“正确”格式。

JSON is not multipart/form-data (which is designed to include files). JSON不是multipart / form-data(旨在包含文件)。

JSON is a text-based data format with a variety of data types (such as strings, arrays, and booleans) but nothing specific for files. JSON是一种基于文本的数据格式,具有多种数据类型(例如字符串,数组和布尔值),但没有特定于文件的数据。

This means that to attach a file, you have to get creative. 这意味着要附加文件,您必须具有创造力。

For example, you could encode a file in text format (eg using base64 ), but it wouldn't be very efficient, and any Word document would result in you getting a much longer string than "UELDMxE76DDKlagmIF5caEVHmJYFv2qF6DpmMSkVPxVdtJxgRYV" . 例如,你可以编码文本格式的文件(例如,使用的base64 ),但它不会是非常有效的,而且会导致你变得比更长的字符串的任何Word文档"UELDMxE76DDKlagmIF5caEVHmJYFv2qF6DpmMSkVPxVdtJxgRYV"

Of course, the method you use to encode the file has to be the method that whatever is reading the JSON expects you to use. 当然,用于编码文件的方法必须是正在读取JSON的任何人希望您使用的方法。 Since there is no standard for this, and you have said nothing about the system which is consuming the JSON you are sending, we have no idea what that method is. 由于对此没有标准,而且您对使用正在发送的JSON的系统一无所知,因此我们不知道该方法是什么。

First of all, I'd recommend reading the postman API docs . 首先,我建议您阅读postman API文档 They have some extremely useful information on there for using the API. 他们那里有一些使用API​​的非常有用的信息。 Two particular articles that might of interest here are these: 以下是两篇有趣的文章:

Looking at it and running it through a validator like this one shows that there are no syntax errors so it must be to do with the JSON parameters the API is expecting. 查看并通过像这样的验证器运行它表明没有语法错误,因此它必须与API期望的JSON参数有关。


Here's something you can try: 您可以尝试以下方法:

In postman, set method type to POST . 在邮递员中,将方法类型设置为POST

Then select Body -> form-data -> Enter your parameter name (file according to your code) 然后选择主体 -> 表单数据 ->输入您的参数名称(根据您的代码文件)

and on right side next to value column, there will be dropdown "text, file" , select File . 和旁边的值列右侧,会出现下拉菜单“文本文件”,选择文件 choose your image file and post it. 选择您的图像文件并发布。

For rest of "text" based parameters, you can post it like normally you do with Postman. 对于其他基于“文本”的参数,您可以像平常对Postman一样进行发布。 Just enter parameter name and select "text" from that right side dropdown menu and enter any value for it, hit send button. 只需输入参数名称,然后从该右侧的下拉菜单中选择“文本” ,然后为其输入任何值,请点击发送按钮。 Your controller method should get called. 您的控制器方法应被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM