Search code examples
pythoncopyleaks-api

400 Bad request, PUT create Url, Api Copyleaks


when trying to create a new scan through a URL it is giving 400 bad request error, accusing that the URL field is mandatory. But I send all the data when I run the postman send correctly.

Print return request:"{u'url': [u'The url field is required.']} 400"

Follow my code:

def creat_url(self, prova): 
    urlPUT = 'https://api.copyleaks.com/v3/education/submit/url/%s' % (prova)

    headers = {
      Consts.AUTHORIZATION_HEADER: self.token.generateAuthrizationHeader(),          
      'Content-type': 'application/json',
    }

    varUrl = "http://my_address/completed/%s" % (prova)
    data = {
        "url": varUrl,
        "properties": {
        "webhooks": {
            "status": varUrl,
            },
        },
    }
    print(data)

    # data_json = json.dumps(data)        
    response = requests.put(urlPUT, headers=headers, data=data)
    print(response.json())
    print(response.status_code)

    if (response.status_code == Consts.HTTP_SUCCESS):
        return response.json()
    else:
        raise CommandFailedError(response)

Solution

  • 'Content-type': 'application/json'
    

    That header promises to send json data, but you aren't actually doing that.

    Use json=data instead of data=data in the requests.put() call.