Integration Nfse São Paulo City Hall returns Bad Request

Asked

Viewed 980 times

0

I am developing a system in PHP to generate Nfse that uses the Web Service of the city of São Paulo, but I am not able to integrate with the Web Service. Whenever I send an XML message to the Web Service, I receive a reply with the following information:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/soap+xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 21 Jun 2018 13:25:20 GMT
Content-Length: 0

To make the test request, I am using Soapui 5.2.1.

Example request "Query" giving error:

<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConsultaCNPJRequest xmlns="http://www.prefeitura.sp.gov.br/nfe">
      <VersaoSchema>1</VersaoSchema>
      <MensagemXML>
          <PedidoConsultaCNPJ>
              <Cabecalho Versao="1">
                <CPFCNPJRemetente>
                  <CNPJ>CNPJ x</CNPJ>
                </CPFCNPJRemetente>
              </Cabecalho>
            <CNPJContribuinte>
            <CNPJ>CNPJ Y</CNPJ>
            </CNPJContribuinte>
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
              <SignedInfo>
                  <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
                    <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
                  <Reference>
                      <Transforms>
                      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                      <Transform Algorithm="http://www.w3c.org/TR/2001/REC-xml-c14n-20010315"/>
                      </Transforms>
                      <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                      <DigestValue>HuMpzsiKXGJw7yduTCts0=</DigestValue>
                  </Reference>
              </SignedInfo>
              <SignatureValue>F0Te8gclpt0pW0BNBPCxknBFXs+igy/VvSM6zvy1agZGcv7ZiYWjzdMNB4G4ANeBPtpWI0hDae0/ZNHzXvUSHUJ+V+PANNCsKj1IAKNn4Sbv1DV4gPj15p4SUzpE4Lee5LvD37fPxx5VALRcWKLQoPL+ViOG+4S5npVcXpurr8HUZvhVkA3ilvNJMGf701q0oCJ/ttil0wo8xl2MaBoqywaTLD1PN5AjS/koFo7A+/ozOSCzIdojXWTIk2NLugYATZ9Trrrckx7TJb3QkBvj6TQg9H6r64Qy9bLxZWIhETrou/lCIAqXqsNvi8wsBYAwcF+R18CsqDw==</SignatureValue>
            <KeyInfo>
                <X509Data>
                </X509Certificate>
                  exemplo1
                </X509Data>
            </KeyInfo>
            </Signature>
          </PedidoConsultaCNPJ>
      </MensagemXML>
    </ConsultaCNPJRequest>
  </soap12:Body>
</soap12:Envelope>

Does anyone have an example of a valid request? Or do you know why this error is occurring?

3 answers

1

I also have the same problem to develop a Python requirement. I can do for SEFAZ using the webservices of the State Government of SP (https://portal.fazenda.sp.gov.br/servicos/nfe/Paginas/URL-WEBSERVICES.aspx)

But I can not for the Million Note (City of Sao Paulo), besides the envelope schema is different, I receive STATUS 400 (BAD REQUEST) but does not return any error information. I called the city hall and nothing.

In the web service link details the following structure:

https://nfe.prefeitura.sp.gov.br/ws/lotenfe.asmx?op=ConsultaCNPJ

POST /Ws/lotenfe.asmx HTTP/1.1 Host: nfe.prefeitura.sp.gov.br Content-Type: application/Soap+xml; charset=utf-8 Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConsultaCNPJRequest xmlns="http://www.prefeitura.sp.gov.br/nfe">
      <VersaoSchema>int</VersaoSchema>
      <MensagemXML>string</MensagemXML>
    </ConsultaCNPJRequest>
  </soap12:Body>
</soap12:Envelope>

Apparently your envelope is correct, make sure your signature with your certificate is correct in your xml.

0

here I still can’t, I am using Postman to make an application, I get the error of Bad Request 400, but without any error message, so it is difficult to solve.

My envelope is in the following format:

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ConsultaCNPJRequest xmlns="http://www.prefeitura.sp.gov.br/nfe">
      <VersaoSchema>1</VersaoSchema>
      <MensagemXML>
        <Cabecalho>
        <Versao>"1"</Versao>
    <CNPJRemetente>
      <CNPJ>NUMERO</CNPJ>
    </CNPJRemetente>
    </Cabecalho>
  <CNPJContribuinte>
    <CNPJ>NUMERO</CNPJ>
  </CNPJContribuinte>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI=""><Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/></Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>pT6zm/meXT3rusPi6VAEaQ68IFQ=</DigestValue></Reference>
</SignedInfo><SignatureValue>"ASSINATURA BASE64"</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>CONTEUDO DO CERTIFICADO EM BASE64</X509Certificate>
</X509Data></KeyInfo>
</Signature>
</MensagemXML>
</ConsultaCNPJRequest>
  </soap12:Body>
</soap12:Envelope>
  • If you send XML in this format via Postman will not work, you have to convert to string. In this conversion, XML is escaped (escaped).

  • Daniel, thank you for the information... I am doing this way, without line break and formatting by Postman and in Python Request too, however I keep getting Bad Request without any return on the error. Would you have some functional example for me to compare please? Obg! Hugs

0

The SP City Hall integration expects to receive an XML transformed into a string, so my XML sent via Soap UI did not work. In php just use the saveXML method to do this transformation after signing XML

Browser other questions tagged

You are not signed in. Login or sign up in order to post.