sandbox fax send: 400 "Bad Request"

  • 1
  • Question
  • Updated 2 years ago
Here is output from my script which shows how the python requests class is building my request. I've compared this to the examples online and I don't see what's wrong with it. I have stripped out the actual html content and modified the destination fax number to hide where it's really going.

submitting content type: multipart/form-data; boundary=6b4176ac3af14157b06702ace82f6ad3
submitting data: ```--6b4176ac3af14157b06702ace82f6ad3
Content-Disposition: form-data;
Content-Type: application/json

{"to": [{"phoneNumber": "+15555555555"}], "faxResolution": "High", "coverIndex": 0}
--6b4176ac3af14157b06702ace82f6ad3
Content-Disposition: form-data; name="file"; filename="T0000001.htm"
Content-Type: text/html

(html is here)
--6b4176ac3af14157b06702ace82f6ad3-- ``` url=`https://platform.devtest.ringcentral....` status code=400 content type=application/json;charset=UTF-8 encoding=UTF-8 text=`{ "message" : "Bad Request", "errors" : [ ] }`
Here is the code generating that output:

	def SubmitFax ( self, fax_destination_number, html_file_name, html_file_content ):
		access_token = self.GetAccessToken()
		
		j = {
			"to": [{"phoneNumber": fax_destination_number}],
			"coverIndex": 0,
			"faxResolution": "High",
		}
		files = {
			'': ('', json.dumps(j), 'application/json'),
			'file': (html_file_name, html_file_content, 'text/html')
		}
		if True:
			e = requests.models.RequestEncodingMixin
			body,content_type = e._encode_files(files,None)
			print("submitting content type: {}".format(content_type))
			print("submitting data: ```{}```".format(body))
		if False:
			log('{0}: submitting data \"\"\"{1}\"\"\"'.format(html_file_name, html_file_content))
		
		headers = {
			'User-Agent': self.user_agent,
			'Authorization': 'Bearer {}'.format ( access_token )
		}
		url = '{self.url}/restapi/v1.0/account/~/extension/~/fax'.format ( **locals() )
		print('url=`{}`'.format(url))
		r = requests.post ( url, headers=headers, files=files )
		if False:
			example_error_response = """{
  "message" : "Bad Request",
  "errors" : [ ]
}`"""
		#j = json.loads(r.text)
		if True:
			print('status code={}'.format(r.status_code))
			print('content type={}'.format(r.headers.get('content-type')))
			print('encoding={}'.format(r.encoding))
			print("text=`{}`".format(r.text))
			return None
Photo of BPE Inc

BPE Inc

  • 100 Points 100 badge 2x thumb

Posted 2 years ago

  • 1
Photo of John Wang

John Wang, Official Rep

  • 5,278 Points 5k badge 2x thumb
There may be issues with your fax MIME body. Compare your request to the ones in the following example page:

http://ringcentral-sdk-ruby.readthedocs.org/en/latest/usage/messages/Fax-Messages/#http-request-exam...
Photo of BPE Inc

BPE Inc

  • 100 Points 100 badge 2x thumb
I found the problem. The example code I found online for changing the content type from multipart/mime to multipart/mixed had a bug that was clobbering the boundary tag.
Photo of John Wang

John Wang, Official Rep

  • 5,278 Points 5k badge 2x thumb
Glad to hear the problem is solved.

The PHP and JavaScript SDKs have fax helper libraries, but the Python SDK doesn't yet. If you're interested in creating a generic one, it may be worthwhile to send a pull request.
(Edited)