MPRester REST query error 405

Hello,

I have a problem that I am struggling to figure out, regarding the MPRester and querying from Materials Project (in legacy MP).

from pymatgen.ext.matproj import MPRester
qe = MPRester('My_api_key')
test1 = qe.get_structure_by_material_id('mp-18640')
test2 = qe.get_entry_by_material_id('mp-18640', property_data='energy')
test3 = qe.get_entries_in_chemsys(['Li', 'O'], inc_structure=True)

While the test1 works, test2 and test3 does not work. However, the code test3 used to work without any problems until recently. And it seems that the fact that test1 works for me means that it’s not related to my API key.

The specific error message for test2 and test3 are as follows:

pymatgen.ext.matproj.MPRestError: REST query returned with error status code 405. Content: b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n'

I’ve searched what 405 REST query error means, and it says that “the server knows the request method, but the target resource doesn’t support this method”. Could you provide some help on why this error is occuring nowadays, and how to fix this? Since this kind of code has been working for more than two years and worked until last month, can it be that there is an issue of the server responding to queries? Also wondering if this issue is related to the transition to new MP from legacy.

Any kind of help will be very appreciated! Thank you!

Best,
KyuJung

Hi KyuJung,

you’re right that this is related to the transition to our new MP website but only happens if you’re sending the request from within the LBNL network (VPN, onsite, or logged into NERSC). We’ve identified the issue and the fix is pending to be deployed. In the meantime, you can force the MPRester to connect to the legacy API by setting the endpoint argument:

MPRester(endpoint="https://legacy.materialsproject.org/rest/v2")

HTH
Patrick

1 Like

Got it, Thank you for your help!

Best,
KyuJung

Hello,

I am having a similar problem. When I run the get_random_packed() function from mpmorph, I get the MPRestError with error code 405. I tried forcing the MPRester to connect to the legacy API, but still seem to get the same error. Is there any other way to work around this issue?

Here is the code I am running:

import numpy as np
from pymatgen.core import Composition, Structure
from mpmorph.runners.amorphous_maker import get_random_packed
from mpmorph.workflows.converge import get_converge_wf

from pymatgen.ext.matproj import MPRester
MPRester(endpoint="https://legacy.materialsproject.org/rest/v2")

target_atoms = 180
comp = 'ZrO2'
c = Composition(comp)
s = get_random_packed(c, target_atoms=target_atoms)

Here is the error I get:
MPRestError: REST query returned with error status code 405. Content: b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>405 Method Not Allowed</title>\n<h1>Method Not Allowed</h1>\n<p>The method is not allowed for the requested URL.</p>\n'

Hi MacKinzie,

thanks for reporting this! The issue with the get_random_packed function in mpmorph is this line in the code. A new MPRester is instantiated with default arguments. The MPRester instance you create in your code is not used. @Jianli_Cheng might be able to implement a change that allows either the external MPRester arguments or the instance itself to be used instead of re-instantiating a default MPRester within the function. Or as a temporary fix, you could add the endpoint argument in the above line yourself.

That being said, it should work without you having to explicitly set the legacy endpoint. I can reproduce the error and will report back when we’ve managed to make the redirect work.

HTH
Patrick

I just merged a change into mpmorph that allows for you to pass an MPRester into the function.

Slight modification to your code to get this to work.

import numpy as np
from pymatgen.core import Composition, Structure
from mpmorph.runners.amorphous_maker import get_random_packed
from mpmorph.workflows.converge import get_converge_wf

from pymatgen.ext.matproj import MPRester

mpr = MPRester(endpoint="https://legacy.materialsproject.org/rest/v2")

target_atoms = 180
comp = 'ZrO2'
c = Composition(comp)
s = get_random_packed(c, target_atoms=target_atoms, mpr=mpr)

Thank you! The code is running now.

Hi MacKinzie and KyuJung,

using the default MPRester endpoint now also works from within the LBNL network.

best,
Patrick